package com.ouwen.smartpay.service.chain.slots.check;

import com.fm.common.emuns.ResponseCode;
import com.fm.core.common.enums.TranStatus;
import com.fm.core.common.exception.OrderCheckException;
import com.fm.core.common.facade.BizResultBase;
import com.ouwen.smartpay.openapi.order.ServiceOrder;
import com.ouwen.smartpay.service.chain.context.Context;
import com.ouwen.smartpay.service.chain.slotchain.AbstractLinkedProcessorSlot;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author <a href="http://youngitman.tech">青年IT男</a>
 * @version v1.0.0
 * @className OrderCheckSlot
 * @description 订单统一请求报文校验
 * @JunitTest: {@link  }
 * @date 2020-04-23 13:58
 **/
public class OrderCheckSlot extends AbstractLinkedProcessorSlot<ServiceOrder> {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public void entry(Context context, ServiceOrder order, Object... args) throws Throwable {

        try {
            //参数校验
            order.check();

            //请求报文体不能为空
            if (order.getDatas() == null || order.getDatas().size() == 0) {

                throw new OrderCheckException(new RuntimeException("请求参数体【datas】不能为空"));
            }

            fireEntry(context, order, args);

        } catch (Exception e) {

            logger.info("订单公共参数校验异常:", e);

            BizResultBase result = new BizResultBase();

            result.setRespCode(ResponseCode.FAIL.code());

            result.setRespMsg(ResponseCode.FAIL.message());

            result.setTranStatus(TranStatus.FTS03);

            result.setTranDesc(TranStatus.FTS03.getMessage());

            result.setDescription(e.getMessage());

            exit(context, order, args, result);

        }

    }

    @Override
    public void exit(Context context, Object... args) {

        fireExit(context, args);
    }
}
